//入口函数
$(function () {
    getUserInfo();

    //退出按钮事件
    let layer = layui.layer;
    $('#btnLogout').on('click', function () {
        layer.confirm('是否确认退出?', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token');
            location.href = '/login.html';
            layer.close(index);
        });
    })
})


//获取用户信息封装函数
function getUserInfo() {
    $.ajax({
        url: '/my/userinfo',
        headers: {
            Authorization: localStorage.getItem('token') || '',
        },
        success: function (res) {

            if (res.status != 0) {
                return layui.layer.msg(res.message);
            };
            renderAvatar(res.data);
        }
    })
}

function renderAvatar(user) {
    let name = user.nickname || user.username;
    $("#welcome").html('欢迎&emsp;' + name);
    if (user.user_pic != null) {
        $(".layui-nav-img").show().attr('src', user.suer_pic);
        $('.text-avatar').hide()
    } else {
        $('.layui-nav-img').hide();
        let text = name[0].toUpperCase();
        $('.text-avatar').show().html(text);
    }
}